﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyX.Core
{
    [System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
    public sealed class XAllowNullAttribute : Attribute
    {
        public XAllowNullAttribute()
        {

        }

    }

    [System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    public abstract class XVaildValueAttribute : Attribute
    {
        public XVaildValueAttribute(object defaultValue)
        {
            _defaultValue = defaultValue;
        }

        public abstract bool IsVaild(object value);

        protected object _defaultValue;
    }

    [System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    public class XVaildEqualAttribute : XVaildValueAttribute
    {
        public XVaildEqualAttribute(object defaultValue) : base(defaultValue)
        {

        }

        public override bool IsVaild(object value)
        {
            return _defaultValue.Equals(value);
        }
    }

    [System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    public class XVaildNotEqualAttribute : XVaildValueAttribute
    {
        public XVaildNotEqualAttribute(object defaultValue) : base(defaultValue)
        {
        }

        public override bool IsVaild(object value)
        {
            return !_defaultValue.Equals(value);
        }
    }

    [System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    internal class XVaildVersionAttribute : XVaildValueAttribute
    {
        public XVaildVersionAttribute() : base(0)
        {

        }

        public override bool IsVaild(object value)
        {
            try
            {
                long.Parse(value.ToString());
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }

}
